<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Session;
// session_start();
class Login extends Controller
{
    //登录
    public function getlogin(){
        //友情链接遍历
        $links=Db::query("select linksname,linksaddress from links where status=0");
    	// echo '1111';exit;
        return $this->fetch("Login/login",['links'=>$links]);
    }
    //检测号码是否存在
    public function getchecktel(){
        //请求对象
        $request = request();
        $tel = $request->get("tel");
        //做手机号码对比
        $arr = Db::table("member")->column("tel");
        if(in_array($tel,$arr)){
            echo '1';
        }else{
            echo '0';
        }
    }
    //执行登录
    public function postdologin(){
        //创建请求
        $request = request();
        //获取注册信息
        $tel=$request->param('tel');
    	$password=$request->param('password');
        $result = Db::table('member')->where("tel='{$tel}' and password='{$password}'")->select();
        if($result){
        	// $_SEESION['tel']=$tel;
            $uid = $result[0]['id'];
            // var_dump($result);exit;
            Session::set('uid',$uid); 
            Session::set('tel',$tel);
            $this->success("登录成功","/home/index");
        }else{
            $this->error("登录失败","/homelogin/login");
        }
    }
    //退出登录
    public function getloginout(){
        //删除登录的session信息
        Session::delete('tel');
        Session::delete('uid');
        if(empty(Session::get('tel'))&&empty(Session::get('uid'))){
            $this->success("退出成功","/home/index");
        }else{
            $this->error("退出失败","/home/index");
        }
    }
    //加载找回密码页面
    public function getfind(){
        //友情链接遍历
        $links=Db::query("select linksname,linksaddress from links where status=0");
        // echo '1111';exit;
        return $this->fetch("Login/find",['links'=>$links]);
    }
    //检测邮箱
    public function getcheckemail(){
        //请求对象
        $request = request();
        $email = $request->get("email");
        // echo $email;exit;
        //做邮箱对比
        $arr = Db::table("member")->column("email");
        // echo $arr;exit;
        if(in_array($email,$arr)){
            echo '1';
        }else{
            echo '0';
        }
    }
    //找回密码
    public function getsendss(){
        //请求对象
        $request = request();
        $email = $request->get("email");
        // echo $email;exit;
        
        $s = sendmailss($email,"找回密码","<a href='http://www.jindebo.com/homelogin/edit?email={$email}'>修改密码</a>");

        if($s){
            $this->success("邮件发送成功,请登录邮箱进行密码修改","https://mail.qq.com/");
        }
    }
    //修改密码页面
    public function getedit(){
        // var_dump($_GET);exit;
        $email = $_GET['email'];
        $data = Db::table("member")->where("email='{$email}'")->select();
        $id = $data[0]['id'];
        // var_dump($data);
        // echo $id;
        //友情链接遍历
        $links=Db::query("select linksname,linksaddress from links where status=0");
        // echo '1111';exit;
        return $this->fetch("Login/edit",['links'=>$links],['id'=>$id]);
    }
    // 执行修改密码
    public function postupdate(){
        // var_dump($_POST);
        //修改密码
        $data['password']=$_POST['password'];
        $id = $_POST['sb'];
        // echo $id;exit;
        if(Db::table('member')->where("id='{$id}'")->update($data)){
            $this->success("修改密码成功","http://www.jindebo.com/homelogin/login");
        }else{
            $this->error("修改密码","http://www.jindebo.com/homelogin/find");
        }
    } 
}
